package com.healthcloud.controller.modules.testresult;

import com.healthcloud.entity.*;
import com.healthcloud.entity.patient.PatientDataEntity;
import com.healthcloud.entity.testresult.TestresultVo;
import com.healthcloud.service.*;
import com.healthcloud.service.patient.PatientDataService;
import com.healthcloud.utils.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 血脂
 */
@Controller
@RequestMapping("bf")
public class BloodFatTestresultController {

	@Autowired
    private TestresultService testresultService;

	@Autowired
	private PatientDataService patientDataService;

    @ApiOperation(value = "血脂信息新增页面")
    @RequestMapping(value = "/toAddBfInfo", method = RequestMethod.GET)
    public String toAddBfInfo(){
        return "admin/archives/addBfInfo";
    }

    @ApiOperation(value = "新增血脂信息")
    @RequestMapping(value = "/addBfInfo", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes addBfInfo(@ApiParam(required = true, name = "idCard", value = "身份证号")
                             @RequestParam("idCard") String idCard,
                            @ApiParam(required = true, name = "tc", value = "总胆固醇")
                             @RequestParam("tc") Double  tc,
                            @ApiParam(required = true, name = "tg", value = "三酰甘油")
                             @RequestParam("tg") Double  tg,
                            @ApiParam(required = true, name = "hdl", value = "高密度脂蛋白胆固醇")
                                 @RequestParam("hdl") Double  hdl,
                            @ApiParam(required = true, name = "ldl", value = "低密度脂蛋白胆固醇")
                                 @RequestParam("ldl") Double  ldl,
                            @ApiParam(required = true, name = "hcy", value = "同型半胱氨酸")
                                 @RequestParam("hcy") Double  hcy,
                            @ApiParam(required = true, name = "apoAI", value = "载脂蛋白AI")
                                 @RequestParam("apoAI") Double  apoAI,
                            @ApiParam(required = true, name = "apoB", value = "载脂蛋白B")
                                 @RequestParam("apoB") Double  apoB,
                            @DateTimeFormat(pattern = DateUtils.DATE_TIME_PATTERN)
                             @ApiParam(required = true, name = "testTime", value = "检测时间")
                             @RequestParam("testTime")  Date testTime) {
        TestresultEntity entity = new TestresultEntity();
        entity.setTestTime(testTime);
        entity.setIdCard(idCard);
        entity.setTestKindCode("502");
        entity.setTestKindName("血脂");
        entity.setDataType(20);
        entity.setIsPublish(1);
        entity.setCreateTime(new Date());

        entity.setTestItemCode("502001");
        entity.setTestItemName("总胆固醇");
        entity.setTestValue(String.valueOf(tc));
        testresultService.save(entity);

        entity.setTestItemCode("502002");
        entity.setTestItemName("三酰甘油");
        entity.setTestValue(String.valueOf(tg));
        testresultService.save(entity);

        entity.setTestItemCode("502003");
        entity.setTestItemName("高密度脂蛋白胆固醇");
        entity.setTestValue(String.valueOf(hdl));
        testresultService.save(entity);

        entity.setTestItemCode("502004");
        entity.setTestItemName("低密度脂蛋白胆固醇");
        entity.setTestValue(String.valueOf(ldl));
        testresultService.save(entity);

        entity.setTestItemCode("502005");
        entity.setTestItemName("同型半胱氨酸");
        entity.setTestValue(String.valueOf(hcy));
        testresultService.save(entity);

        entity.setTestItemCode("502006");
        entity.setTestItemName("载脂蛋白AI");
        entity.setTestValue(String.valueOf(apoAI));
        testresultService.save(entity);

        entity.setTestItemCode("502007");
        entity.setTestItemName("载脂蛋白B");
        entity.setTestValue(String.valueOf(apoB));
        testresultService.save(entity);

        /**
         * 病人最后一次数据回显
         */
        PatientDataEntity data = new PatientDataEntity();
        data.setIdCard(entity.getIdCard());
        data.setTriglyceride(String.valueOf(tg));
        data.setCholesterol(String.valueOf(tc));
        data.setHighCholesterol(String.valueOf(hdl));
        data.setLowCholesterol(String.valueOf(ldl));
        patientDataService.saveOrUpdate(data);
        return ApiRes.SUCCESS;
    }

    @ApiOperation(value = "血脂信息更多页面")
    @RequestMapping(value = "/toMoreBfInfo", method = RequestMethod.GET)
    public String toMoreBfInfo(){
        return "admin/archives/moreBfInfo";
    }

    @ApiOperation(value = "血脂测试列表查询")
    @RequestMapping(value = "/patientBfTests",method = RequestMethod.POST)
    @ResponseBody
    public MsgVo patientBpTests(PageParam pageParam, TestresultVo entity){
        List list = testresultService.queryPatientBfTests(entity, pageParam);
        int count = testresultService.queryPatientBfTestsCount(entity);
        return new MsgVo(list, count, count, pageParam.getsEcho());
    }

}
